
package de.carcophan.jimdb.jsf.security;

import de.carcophan.jimdb.ejb.entites.User;
import de.carcophan.jimdb.ejb.entites.service.UserFacade;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;


@SessionScoped
public class AuthenticatedUser implements Serializable {

    private User user;
    @EJB
    UserFacade userFacade;

  
    private void loadAuthenticatedUser() {
        final String authenticatedRemoteUser = getAuthenticatedRemoteUser();
        System.out.println("String authenticatedRemoteUser = "+authenticatedRemoteUser);
        if (authenticatedRemoteUser != null) {
            this.user = userFacade.findByLoginId(authenticatedRemoteUser);
        }
    }

    public void setUser(User user) {
        this.user = user;
    }

    public User getUser() {
        if (this.user==null){
            loadAuthenticatedUser();
        }

        return this.user;
    }

    public FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    private String getAuthenticatedRemoteUser() {
        return getFacesContext().getExternalContext().getRemoteUser();
    }
}
